/**   
 * @Title: TestTypeDaoImplementation.java 
 * @Package cn.edu.cdtu.healthserver.dao.impl 
 * @Description: TODO 
 * @author  
 * @date 
 * @version V0.0   
 */
package cn.edu.cdtu.healthserver.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import cn.edu.cdtu.healthserver.dao.SQLToken;
import cn.edu.cdtu.healthserver.dao.TestTypeDao;
import cn.edu.cdtu.healthserver.domain.TestType;
import cn.edu.cdtu.healthserver.utils.C3P0Util;

/** 
 * @ClassName: TestTypeDaoImplementation 
 * @Description: TODO
 * @author 
 * @date 
 *  
 */
public class TestTypeDaoImplementation implements TestTypeDao {
	
	private static final String TABLE_STRING = " test_type ";
	private static final String ATTRIBUTE_ID_STRING = " id ";
	private static final String ATTRIBUTE_TYPE_STRING = " type ";
	private static final String ATTRIBUTE_DESCRIPTION_STRING = " description ";
	private static final String ATTRIBUTE_AVAILABLE_STRING = " available ";
	
	/**
	 * 数据库查询器
	 */
	private QueryRunner queryRunner = 
			new QueryRunner(C3P0Util.getDataSource());

	/* (non-Javadoc)
	 * <p>Title: add</p> 
	 * <p>Description: </p> 
	 * @param object
	 * @return 
	 * @see cn.edu.scu.healthserver.dao.ObjectQueryDao#add(java.lang.Object)
	 */
	@Override
	public boolean add(TestType object) {
		// TODO Auto-generated method stub
		final String sqlString = ""
				+ SQLToken.INSERT_INTO_STRING
				+ TABLE_STRING 
				+ "(" 
				+ ATTRIBUTE_TYPE_STRING + ","
				+ ATTRIBUTE_DESCRIPTION_STRING
				+ ")" 
				+ SQLToken.VALUES_STRING + "(?,?)";
		Object[] paramsObject = new Object[]{
				object.getType(),object.getDescription()
		};
		try {
			int result = queryRunner.update(sqlString, paramsObject);
			if(result > 0){
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	/* (non-Javadoc)
	 * <p>Title: update</p> 
	 * <p>Description: </p> 
	 * @param object
	 * @return 
	 * @see cn.edu.scu.healthserver.dao.ObjectQueryDao#update(java.lang.Object)
	 */
	@Override
	public boolean update(TestType object) {
		// TODO Auto-generated method stub
		final String sqlString = ""
				+ SQLToken.UPDATE_STRING
				+ TABLE_STRING
				+ SQLToken.SET_STRING
				+ ATTRIBUTE_TYPE_STRING + "= ? ,"
				+ ATTRIBUTE_DESCRIPTION_STRING + "= ?,"
				+ ATTRIBUTE_AVAILABLE_STRING + "= ?"
				+ SQLToken.WHERE_STRING
				+ ATTRIBUTE_ID_STRING + "= ?";
		Object[] paramsObject = new Object[]{
				object.getType(),object.getDescription(),
				object.getAvailable(),object.getId()
		};
		try {
			int result = queryRunner.update(sqlString, paramsObject);
			if(result > 0){
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	/* (non-Javadoc)
	 * <p>Title: delete</p> 
	 * <p>Description: </p> 
	 * @param object
	 * @return 
	 * @see cn.edu.scu.healthserver.dao.ObjectQueryDao#delete(java.lang.Object)
	 */
	@Override
	public boolean delete(TestType object) {
		// TODO Auto-generated method stub
		final String sqlString = ""
				+ SQLToken.DELETE_FROM_STRING
				+ TABLE_STRING
				+ SQLToken.WHERE_STRING
				+ ATTRIBUTE_ID_STRING + "= ?";
		Object[] paramsObject = new Object[]{
			object.getId()	
		};
		try {
			int result = queryRunner.update(sqlString, paramsObject);
			if(result > 0){
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	/* (non-Javadoc)
	 * <p>Title: queryALL</p> 
	 * <p>Description: </p> 
	 * @return 
	 * @see cn.edu.scu.healthserver.dao.TestTypeDao#queryALL()
	 */
	@Override
	public List<TestType> queryALL() {
		// TODO Auto-generated method stub
		final String sqlString = ""
				+ SQLToken.SELECT_STRING + "*"
				+ SQLToken.FROM_STRING + TABLE_STRING;
		try {
			return queryRunner.query(sqlString,
					new BeanListHandler<TestType>(TestType.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException();
		}
	}

}
